/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  							 script_material_action.cpp

	$Header: /heroes4/script_material_action.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "script_material_action.h"

#include "script_targeting.h"
#include "enum_operations.h"
#include "materials.h"
#include "sized_int_types.h"
#include "streambuf_operators.h"
#include "adventure_map.h"
#include "player.h"
#include "basic_dialog.h"
#include "owned_adv_object.h"
#include "town.h"

// --------------------------------------------------------------------------
// t_script_material_action members
// --------------------------------------------------------------------------

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------

bool t_script_material_action::read( std::streambuf& stream, int version )
{
	if ( !t_script_targeted_action< t_target >::read( stream, version ) )
		return false;

	t_material material;
	for (	material = t_material( 0 );
			material < k_material_count;
			enum_incr( material ) )
		m_materials[ material ] = get< t_uint32 >( stream );

	return true;
}

bool t_script_material_action::write( std::streambuf& stream ) const
{
	if ( !t_script_targeted_action< t_target >::write( stream ) )
		return false;

	t_material material;
	for (	material = t_material( 0 );
			material < k_material_count;
			enum_incr( material ) )
		put< t_uint32 >( stream, m_materials[ material ] );

	return true;
}


void t_script_material_action::add_icons(t_basic_dialog* dialog) const
{
	dialog->add_materials(m_materials);
}

bool t_script_take_material::read_from_map(
	std::streambuf &	stream,
	int					format_version )
{
	if ( !t_script_targeted_action< t_target >::read_from_map( stream, format_version ) )
		return false;

	t_material material;
	for (	material = t_material( 0 );
			material < k_material_count;
			enum_incr( material ) )
		m_materials[ material ] = -(get< t_uint32 >( stream ));

	return true;
}

bool t_script_give_material::read_from_map(
	std::streambuf &	stream,
	int					format_version )
{
	if ( !t_script_targeted_action< t_target >::read_from_map( stream, format_version ) )
		return false;

	t_material material;
	for (	material = t_material( 0 );
			material < k_material_count;
			enum_incr( material ) )
		m_materials[ material ] = get< t_uint32 >( stream );

	return true;
}

void t_script_material_action::execute(t_script_context_hero const& context) const
{
	t_player* player = select_player_by_target(get_target(), context.map, NULL, context.army_owner, context.opposing_army_owner);
	if (player)
		player->gain(m_materials);
}

void t_script_material_action::execute(t_script_context_town const& context) const
{
	t_player* player = select_player_by_target(get_target(), context.map, context.object, context.garrison ? context.garrison->get_owner() : NULL, context.opposing_army_owner);
	if (player)
		player->gain(m_materials);
}

void t_script_material_action::execute(t_script_context_object const& context) const
{
	t_player* player = select_player_by_target(get_target(), context.map, NULL, NULL, NULL);
	if (player)
		player->gain(m_materials);
}

void t_script_material_action::execute(t_script_context_army const& context) const
{
	t_player* player = select_player_by_target(get_target(), context.map, NULL, context.army_owner, context.opposing_army_owner);
	if (player)
		player->gain(m_materials);
}

void t_script_material_action::execute(t_script_context_global const& context) const
{
	t_player* player = select_player_by_target(get_target(), context.map, NULL, NULL, NULL);
	if (player)
		player->gain(m_materials);
}



